package com.fourth.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fourth.dao.UBOrderDao;
import com.fourth.service.UBOrderService;
import com.ljx.entity.Order;
import com.ljx.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author：lmy
 * @Date：2023/3/3 15:07
 */
@RestController
@RequestMapping("home/api/order")
public class UBOrderController {

    @Autowired
    private UBOrderService ubOrderService;

    @Autowired(required = false)
    private UBOrderDao ubOrderDao;

    @GetMapping("findOrder")
    public Result findOrder(@RequestParam Integer currentPage,@RequestParam Integer pageSize,
                            @RequestParam Integer state,@RequestParam Integer commentstatus){
        return ubOrderService.findOrder(currentPage,pageSize,state,commentstatus);
    }

    @GetMapping("deleteOrder")
    public Result updateOrder(@RequestParam String code){
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("code",code);
        Order order = new Order();
        order.setState(2);
        int row = ubOrderDao.update(order,wrapper);
        if (row != 0){
            return new Result(2000,"删除成功");
        }
        return new Result(5000,"删除失败");
    }

    @GetMapping("cancelOrder")
    public Result cancelOrder(@RequestParam String code){
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("code",code);
        Order order = new Order();
        order.setState(4);
        int row = ubOrderDao.update(order,wrapper);
        if (row != 0 ){
            return new Result(2000,"取消订单成功");
        }
        return new Result(5000,"取消订单失败");
    }


    @GetMapping("updateStateToFailure")
    public Result updateStateToFailure(@RequestParam String code){
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("code",code);
        Order order = new Order();
        order.setState(3);
        int row = ubOrderDao.update(order,wrapper);
        if (row != 0 ){
            return new Result(2000,"订单已超时");
        }
        return new Result(5000,"订单状态更改失败");
    }
}
